package cn.rengy.web.framework.datagrid.web;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.rengy.lang.ResultEntity;
import cn.rengy.web.framework.datagrid.service.DataGridService;


/**
 * 处理easyui datagrid请求
 * 
 */
@RestController
public class DataGridController {
	private static Logger logger = LoggerFactory.getLogger(DataGridController.class);
	
	@Autowired
	private DataGridService dataGridService;
	/**
	 * 列表查询
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/grid/{sqlid}",method = RequestMethod.POST)
	public Map<String,Object> list (@PathVariable("sqlid") String sqlid,@RequestBody Map<String,Object> paramMap){
		return dataGridService.query(sqlid, paramMap);
	}
	@RequestMapping(value="/grid/query",method = RequestMethod.POST)
	public Map<String,Object> unAuthList (@RequestParam("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return dataGridService.query(sqlid, paramMap);
	}
	/**
	 * 列表保存
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/grid/update/{sqlid}",method = RequestMethod.POST)
	public ResultEntity<?> save(@PathVariable("sqlid") String sqlid,@RequestBody Map<String,Object> paramMap){
		dataGridService.save(sqlid, paramMap);
		return ResultEntity.ok();
	}
	/**
	 * 列表保存
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/grid/update/treeGrid/{sqlid}",method = RequestMethod.POST)
	public ResultEntity<?> treeGridSave(@PathVariable("sqlid") String sqlid,@RequestBody Map<String,Object> paramMap){
		dataGridService.treeGridSave(sqlid, paramMap);
		return ResultEntity.ok();
	}
	/**
	 * 字段信息查询
	 * @param tablename
	 * @return
	 */
	@RequestMapping(value="/authenticated/grid/column/{tablename}",method = RequestMethod.POST)
	public ResultEntity<?> querycolumnData(@PathVariable("tablename") String tablename){
		return ResultEntity.ok(dataGridService.queryGridColumn(tablename));
	}
	
	/**
	 * 字段信息查询v2版本
	 * @param tablename
	 * @return
	 */
	@RequestMapping(value="/authenticated/grid/column/v2/{page_id}",method = RequestMethod.POST)
	public ResultEntity<?> querycolumnData2(@PathVariable("page_id") Long page_id){
		return ResultEntity.ok(dataGridService.queryGridColumnV2(page_id));
	}
	
}
